package com.shiwaixiangcun.customer.presenter.impl;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.GlobalConfig;
import com.shiwaixiangcun.customer.entity.LogoutBean;
import com.shiwaixiangcun.customer.entity.User;
import com.shiwaixiangcun.customer.entity.UserInfoBean;
import com.shiwaixiangcun.customer.network.HttpCallBack;
import com.shiwaixiangcun.customer.network.HttpRequest;
import com.shiwaixiangcun.customer.network.NetworkHelper;
import com.shiwaixiangcun.customer.presenter.IInformationPresenter;
import com.shiwaixiangcun.customer.ui.IHouseInformationView;
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr;
import com.shiwaixiangcun.customer.utils.LoginOutUtil;
import com.shiwaixiangcun.customer.utils.RefreshTokenUtil;

import java.util.HashMap;

/**
 * @author Administrator
 * @date 2017/5/25
 */

public class UserInformationImpl implements IInformationPresenter {
    private IHouseInformationView iHouseInformationView;
    private String strContent;

    private NetworkHelper mNetworkHelper;
    public UserInformationImpl(IHouseInformationView iHouseInformationView, String str_content) {
        this.iHouseInformationView = iHouseInformationView;
        this.strContent = str_content;
    }

    @Override
    public void setBgaAdpaterAndClick(Context context) {

        sendInformationHttp(context);

    }

    @Override
    public void setHeadImageXg(Context context) {
        sendHeadImageHttp(context);
    }

    @Override
    public void setSex(Context context) {
        sendSexHttp(context);
    }

    @Override
    public void setData(Context context) {
        sendBirthHttp(context);
    }

    @Override
    public void setLogout(Context context) {
        sendLogoutHttp(context);
    }


    /**
     * 设置个人信息
     *
     * @param context
     */
    private void sendInformationHttp(final Context context) {


        String strToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.TOKEN, "");
        final String strRefreshToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.Refresh_token, "");
        HashMap<String, Object> hashMap = new HashMap<>(2);
        hashMap.put("access_token", strToken);

        HttpRequest.get(GlobalApi.INSTANCE.getInformation(), hashMap, new HttpCallBack() {
            @Override
            public void onSuccess(String responseJson) {
                UserInfoBean informationaBean = new Gson().fromJson(responseJson, UserInfoBean.class);
                if (informationaBean.getResponseCode() == 1001) {
                    iHouseInformationView.setBgaAdpaterAndClickResult(informationaBean);
                } else if (informationaBean.getResponseCode() == 1018) {
                    RefreshTokenUtil.refreshToken(context, strRefreshToken);
                } else if (informationaBean.getResponseCode() == 1019) {
                    LoginOutUtil.sendLoginOutUtil(context);
                }

            }

            @Override
            public void onFailed(Exception e) {
                Log.i("oooooo---onFailed---", e.toString());
            }
        });
    }


    /**
     * 修改头像
     *
     * @param context
     */
    private void sendHeadImageHttp(final Context context) {

        String strToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.TOKEN, "");
        final String refreshToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.Refresh_token, "");
        HashMap<String, Object> hashMap = new HashMap<>(2);
        hashMap.put("access_token", strToken);
        hashMap.put("imageId", strContent);
        HttpRequest.put(GlobalApi.INSTANCE.getModify(), hashMap, new HttpCallBack() {
            @Override
            public void onSuccess(String responseJson) {
                User user = new Gson().fromJson(responseJson, User.class);
                if (user.getResponseCode() == 1001) {
                    iHouseInformationView.setHeadImage(user);
                } else if (user.getResponseCode() == 1018) {
                    RefreshTokenUtil.refreshToken(context, refreshToken);
                } else if (user.getResponseCode() == 1019) {
                    LoginOutUtil.sendLoginOutUtil(context);
                }

            }

            @Override
            public void onFailed(Exception e) {
            }
        });
    }

    /**
     * 设置性别
     *
     * @param context
     */
    private void sendSexHttp(final Context context) {

        String strToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.TOKEN, "");
        final String strRefreshToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.Refresh_token, "");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("access_token", strToken);
        hashMap.put("sex", strContent);
        HttpRequest.put(GlobalApi.INSTANCE.getModify(), hashMap, new HttpCallBack() {
            @Override
            public void onSuccess(String responseJson) {
                User user = new Gson().fromJson(responseJson, User.class);
                if (user.getResponseCode() == 1001) {
                    iHouseInformationView.setSex(user);
                } else if (user.getResponseCode() == 1018) {
                    RefreshTokenUtil.refreshToken(context, strRefreshToken);
                } else if (user.getResponseCode() == 1019) {
                    LoginOutUtil.sendLoginOutUtil(context);
                }
            }

            @Override
            public void onFailed(Exception e) {

            }
        });
    }

    /**
     * 设置生日
     *
     * @param context
     */
    private void sendBirthHttp(final Context context) {
        String strToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.TOKEN, "");
        final String strRefreshToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.Refresh_token, "");
        HashMap<String, Object> hashMap = new HashMap<>(2);
        hashMap.put("access_token", strToken);
        hashMap.put("birthday", strContent);
        HttpRequest.put(GlobalApi.INSTANCE.getModify(), hashMap, new HttpCallBack() {
            @Override
            public void onSuccess(String responseJson) {
                User user = new Gson().fromJson(responseJson, User.class);
                if (user.getResponseCode() == 1001) {
                    iHouseInformationView.setData(user);
                } else if (user.getResponseCode() == 1018) {
                    RefreshTokenUtil.refreshToken(context, strRefreshToken);
                } else if (user.getResponseCode() == 1019) {
                    LoginOutUtil.sendLoginOutUtil(context);
                }
            }

            @Override
            public void onFailed(Exception e) {

            }
        });
    }

    /**
     * 退出登录
     *
     * @param context
     */
    private void sendLogoutHttp(final Context context) {
        String strToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.TOKEN, "");
        String strRefreshToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.Refresh_token, "");
        HashMap<String, Object> hashMap = new HashMap<>(2);
        hashMap.put("access_token", strToken);
        HttpRequest.get(GlobalApi.INSTANCE.getLogout(), hashMap, new HttpCallBack() {
            @Override
            public void onSuccess(String responseJson) {
                LogoutBean user = new Gson().fromJson(responseJson, LogoutBean.class);
                iHouseInformationView.setLogout(user);


            }

            @Override
            public void onFailed(Exception e) {

            }
        });
    }


}
